package com.hdax.dm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.entity.order.DmOrder;
import com.hdax.dm.entity.pay.DmTrade;
import com.hdax.dm.exception.DmException;
import com.hdax.dm.feign.OrderFeign;
import com.hdax.dm.service.PayService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PaySImpl implements PayService {

    @Autowired
    private OrderFeign orderFeign;

    @Override
    @SneakyThrows
    public String pay(String orderNo) {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000119635093",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC4H61FX4HrP2TQTQsT+mBBT3/+h3VtQtnpYFRG7/CiN8AkcfQIuhiNlVAo1DVassfvHTGU5ugVzvJ+cNDhionG4yQPGn+Q8d+BIjhaLZsv8t132ffBqOWNyaHAUF8NJv95wFN3y8e5M0JcmyO4uKAM7dONOeP1UIyw992dxCqMKFgO+hIRBaIA88RZNRPbwlNehvRvg/Qa8vz1LTjw8pG29+V4y1Z6EwBEil2ZzdY27f/Khcisc+qCduITEBklT21nKEvTI1KrbwYSduWbBkf+u6ymadS+bTjzfFU6CXtMxt5GzlSi8+WlNSgYs6AJdR0ZD81Pm38kaZ4xQMkg6OYZAgMBAAECggEBAIWXBiK1XxAOWNkBbS47lScNxXIpWjvCK1U2fS9S26b3CEPpIgRK8ZGYaiaVgXijCoklB1pgBrf8BNlUhVl/KOPS3dDPiwrhUybgaz6SpGwvrBxPW4F5R8SOLPvZ1H04dFgkAOuEQQ7QcC+8qDhYTF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"json",
                "UTF-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhVPqTK/n0McTIvFRLmEGoVhBLEXVF+eq0HpWoJmIcnSrziuKOU9DFYxgKYvqScWJGLlsU/tzK1Jp8lBQvuqhJDBFdyx9wcyr36arsSQoYFlmPF3o+WuyZD1GI9NKC5l2zHszCYj7xj9oS+zxpKeNGx6aJYAlRWEdzohd6eut1DYBwRV1XYkGarSsOqh4IIWUWYUEACIQ9bpmdWTu67NpGnTHja2+9U1QYOnKRY6UndtTae/fR4vafafx46ELkZAuxNKSKJaI1wOmPykEkoKm8LSdJ4RCU5d8TMqyppR9J6nfKMT+dLJL699JA7utKSskJMC/+hVTQIv31ME0SgvNgQIDAQAB",
                "RSA2");
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl("http://localhost:8080");//支付成功后的同步回调
        alipayRequest.setNotifyUrl("http://vh74mh.natappfree.cc/order/payed");//支付成功后的异步回调

        DmOrder order= orderFeign.likeUser(orderNo);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderNo;

        //付款金额，必填
        String total_amount = String.valueOf(order.getTotalAmount());
        //订单名称，必填
        String subject = order.getItemName();
        //商品描述，可空
        String body = "大觅网订单";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        if(order.getOrderType()==-1){
            throw new DmException("5555","订单超时");
        }
        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        return result;
    }

    @Override
    public CommonResponse<Map<String, String>> confirmpay(String orderNo) {
        Map<String,String> map=new HashMap<>();
        DmOrder order= orderFeign.likeUser(orderNo);
        map.put("orderNo",order.getOrderNo());
        map.put("itemName",order.getItemName());
        map.put("totalAmount",order.getTotalAmount().toString());
        return ResponseUtil.returnSuccess(map);
    }
}
